package com.efast.dmom.mes.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.efast.dmom.mes.bean.Part;
import com.efast.dmom.mes.bean.PartTree;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.entity.base.IModificationFlags;
import com.efast.dmom.mes.service.PartService;

import io.swagger.annotations.ApiOperation;

/**
 * To achieve the part to add, delete, update and query
 * 
 * @author wj
 * @Date 2020/1/7
 * @Data WJ modify 2020/2/18
 */

@Controller
@RequestMapping(value = "/part")
public class PartController {

	@Autowired
	private PartService partService;

	@RequestMapping(value = "/operateByPart")
	public @ResponseBody ResultBean operateByPart(@RequestBody Part part) {
		ResultBean resultBean = new ResultBean(true,' ');
		// Judge to add
		if (part.getModificationFlag()== IModificationFlags.NEW) {
			return partService.save(part);
		// Judge to update
		} else if (part.getModificationFlag() == IModificationFlags.MODIFIED) {
			return partService.update(part);
		// Judge to delete
		} else if (part.getModificationFlag() == IModificationFlags.DELETED) {
			return partService.delete(part);
		}
		return resultBean;
	}
	
	@RequestMapping(value = "/batchDelete")
	public @ResponseBody ResultBean batchDelete(@RequestBody List<Part> list) {
		ResultBean batchDelete = partService.batchDelete(list);
		return batchDelete;
	}
	
	//get part by id
	@RequestMapping(value = "/getPartById/{id}")
	public @ResponseBody Part getPartByIfd(@PathVariable long id) {
		return partService.getById(id);
	}
	
	//get part by name
	@ApiOperation(value = "通过物料号和版本获取物料")
	@RequestMapping(value = "/getPartByPartNoAndRevision")
	public @ResponseBody PartTree getPartByPartNoAndRevision(@RequestBody Part part) {
		
		return partService.getPartByPartNoAndRevision(part);
	}
	@RequestMapping(value = "/getAllPart")
	public @ResponseBody List<Part> getAllPart() {
		
		return partService.getAllPart();
	}
	
	@RequestMapping("/validatePartDelete")
	public @ResponseBody ResultBean validatePartDelete (@RequestBody Part part) {
		ResultBean validatePartDelete = partService.validatePartDelete(part);
		return validatePartDelete;
		
	}
	
}
